#define _WINSOCK_DEPRECATED_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")  //load ws2_32.dll

#define BUFF_SIZE 100

int main() {
	//init DLL
	WSADATA wsaData;
	WSAStartup(MAKEWORD(2, 2), &wsaData);

	//sent request to server 
	struct sockaddr_in sockAddr;
	memset(&sockAddr, 0, sizeof(sockAddr));  //fill every element with 0
	sockAddr.sin_family = PF_INET;
	sockAddr.sin_addr.s_addr = inet_addr("169.254.232.64");
	sockAddr.sin_port = htons(8080);


	while (1) {

		//create sock
		SOCKET sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
		connect(sock, (SOCKADDR*)&sockAddr, sizeof(SOCKADDR));


		//get input from keyboard
		char buffer_[BUFF_SIZE] = { 0 };
		printf("Input a string (input 'exit' to exit): ");
		scanf("%s", buffer_);


		// decide data to send
		double array[3];
		array[0] = 0.0;
		array[1] = 0.1;
		array[2] = 0.2;
		char buffer[sizeof(array)];
		memcpy(buffer, array, sizeof(array));



		// send data
		send(sock, buffer, BUFF_SIZE, NULL);

		//get data from server
		char szBuffer[BUFF_SIZE] = { 0 };
		recv(sock, szBuffer, BUFF_SIZE, NULL);

		//print data
		printf("Message form server: %s\n", szBuffer);
		double rcv_array[3];
		memcpy(rcv_array, szBuffer, sizeof(rcv_array));
		printf("I get feedback ");
		for (int j = 0; j < 3; j++)
			printf("%f ", rcv_array[j]);
		printf("\n");

		//close sock
		closesocket(sock);

		if (!strcmp(buffer_, "exit"))
			break;

	}

	//stop using DLL
	WSACleanup();

	system("pause");
	return 0;
}